package com.tenyears.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * Redis工具
 *
 * @author 十年
 * @date 2025/10/25
 */
@Component
public class RedisUtil {
    private static StringRedisTemplate redisTemplate;

    /**
     * 缓存String
     *
     * @param key   key
     * @param value value
     */
    public static void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    /**
     * 缓存String（带有效期）
     *
     * @param key     key
     * @param value   value
     * @param timeout 有效期秒
     */
    public static void set(String key, String value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }

    /**
     * 获取String缓存
     *
     * @param key     key
     */
    public static String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Autowired
    public RedisUtil(StringRedisTemplate redisTemplate) {
        RedisUtil.redisTemplate = redisTemplate;
    }
}
